package com.isky.visual.db.controller;


import com.isky.visual.common.BasePageResult;
import com.isky.visual.db.entity.*;
import com.isky.visual.db.service.ISourceService;
import com.isky.visual.db.vo.param.QueryTableDataParam;
import com.isky.visual.design.sqlparsefactory.ISqlParse;
import com.isky.visual.design.sqlparsefactory.MySqlParse;
import com.isky.visual.design.sqlparsefactory.SqlParseFactory;
import com.isky.visual.interceptor.annotation.LoginValidate;
import com.isky.visual.result.CodeMsg;
import com.isky.visual.result.ResultVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据库连接的配置表 前端控制器
 * </p>
 *
 * @author wudl
 * @since 2020-04-14
 */

@RestController
@RequestMapping("/db/source")
@Api(tags = {"数据源相关接口"})
public class SourceController {

    @Autowired
    ISourceService iSourceService;

    @PostMapping("/getShowSql")
    @ApiOperation("显示sql 语句")
    public ResultVo<String>  getShowSql(@RequestBody DataStoreModel dataStoreModel) {
        return  iSourceService.getShowSql(dataStoreModel);
    }


    @PostMapping("/showTable")
    @ApiOperation("根据数据库id 查询所有的表名")
    @ApiImplicitParams({@ApiImplicitParam(name = "dbId", value = "数据库id", required = true, dataType = "Long", paramType = "query")
            })
    public ResultVo<Object> showTable(@RequestParam(name ="dbId" ) Long dbId) {

        ResultVo<Object> objectResultVo = iSourceService.showTable(dbId);
        return objectResultVo;
    }

    @PostMapping("/showColumn")
    @ApiOperation("根据数据库id 和表名查询列名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dbId", value = "数据库id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "tableName", value = "表名", required = true, dataType = "string", paramType = "query")})
    public ResultVo<Object> showColumn(@RequestParam(name ="dbId" ) Long dbId,@RequestParam(name ="tableName") String tableName) {
        return iSourceService.showColumn(dbId, tableName);
    }

    @PostMapping("/parse")
    @ApiOperation("SQL 的解析")
    public ResultVo<Datastore>  SqlParse(@RequestBody DataStoreModel dataStoreModel) {
        return iSourceService.getSqlParse(dataStoreModel);
    }
    @PostMapping("/customSqlPares")
    @ApiOperation("自定义  SQL 的解析")
    public ResultVo<Datastore>  GetCustomSqlPares(@RequestBody  Map<String, String>  mapdb) throws Exception {
        return  iSourceService.GetCustomSqlPares(mapdb);
    }


    @PostMapping("/GetCustomSqlShowData")
    @ApiOperation("自定义SQL 的解析 的 结果显示")
    public ResultVo<List<Map<String, Object>>>  GetCustomSqlShowData(@RequestBody  Map<String, String>  mapdb) throws Exception {
        return  iSourceService.GetCustomSqlShowData(mapdb);
    }

    @PostMapping(value = "/showDataResult")
    @ApiOperation("显示数据集")
    public ResultVo<Object> showDataResult(@RequestBody DataStoreModel dataStoreModel) {
        return iSourceService.showDataResult(dataStoreModel);
    }


    @GetMapping(value = "/getDbSource")
    @ApiOperation("获取数据源")
    public ResultVo<List<Source>> getDbSource() {
        return iSourceService.getAllSource();
    }


    @PostMapping(value = "/getTableData")
    @ApiOperation("根据数据库id和表名获取数据")
    public ResultVo<BasePageResult>  getTableData(@RequestBody QueryTableDataParam queryTableDataParam){
        ResultVo<BasePageResult> tableData = iSourceService.getTableData(queryTableDataParam);
        return  tableData;
    }

    @PostMapping(value = "/addDbSource")
    @ApiOperation("新增数据源")
    public ResultVo<String> addDbSource(@RequestBody @ApiParam("数据源参数实体") Source source) {
        return iSourceService.saveDbSource(source);
    }

    @PostMapping(value = "/dbConnection")
    @ApiOperation("测试连接的数据源")
    public ResultVo<String> dbConnection(@RequestBody @ApiParam("数据源配置实体") Source source) {
        return iSourceService.dbIsConnection(source);
    }

    @PostMapping(value = "/updateSource")
    @ResponseBody
    @ApiOperation("更新数据")
    public ResultVo<String> updateSource(HttpServletResponse response, @RequestBody Source source) {
        return iSourceService.updateDbSource(response, source);
    }

    @PostMapping(value = "/delSource")
    @ResponseBody
    @ApiOperation("删除数据源2")
    @ApiImplicitParams({
            @ApiImplicitParam(name="dbId", dataType = "Long", value = "数据库id")
    })
    public ResultVo<String> delSource(HttpServletResponse response,@RequestParam(name ="dbId") Long dbId) {
        return iSourceService.delDbSource(response, dbId);
    }

}
